package com.haiyou.common.spring.schedule;

import java.io.Serializable;
import java.util.concurrent.Delayed;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import org.springframework.util.Assert;

import com.haiyou.common.thread.task.AbstractCallableCoreTask;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

/**
 * 
 * @Description: 带回调的基于 Callable 定时任务
 * @author xingyuan
 * @date 2022年7月6日 下午3:41:13
 */
@Getter
public abstract class CallableScheduledTask<V extends Serializable> extends AbstractCallableCoreTask<V>
					implements Delayed,Future<V>,Serializable{

	private static final long serialVersionUID = 2244543077985863624L;

	/**
	 * Futrue
	 */
	@Setter
	private ScheduledFuture<V> futrue;
	
	/**
	 * 校验任务是否提交到定时器
	 */
	private void checksubmit() {
		Assert.notNull(futrue,"task not submit in secheduled");
	}

	@Override
	public int compareTo(Delayed o) {
		checksubmit();
		return futrue.compareTo(o);
	}

	@Override
	public boolean cancel(boolean mayInterruptIfRunning) {
		checksubmit();
		return futrue.cancel(mayInterruptIfRunning);
	}

	@Override
	public boolean isCancelled() {
		checksubmit();
		return futrue.isCancelled();
	}

	@Override
	public boolean isDone() {
		checksubmit();
		return futrue.isDone();
	}

	@Override
	public V get() throws InterruptedException, ExecutionException {
		checksubmit();
		return futrue.get();
	}

	@Override
	public V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
		checksubmit();
		return futrue.get(timeout, unit);
	}

	@Override
	public long getDelay(TimeUnit unit) {
		checksubmit();
		return futrue.getDelay(unit);
	}

}
